/*  PrimeBlaster
    by Kjetil Eide 
    kjetile445@gmail.com

    This file is part of PrimeBlaster

    PrimeBlaster is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PrimeBlaster is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PrimeBlaster.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.eide.spaceship;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

/**
 * Draw graphics to screen 
 * @author kjetil
 *
 */
public class SpaceGraphics {
	private int status_x;
	private int status_y;
	private int width;
	private int height;
	
	Image background;
	Image statusBar;
	Image healthIcon;
	private int bg_x;
	private int bg_y;
	
	//private Image instructions;
	
	/**
	 * Draw graphics to screen 
	 * @param width width of screen 
	 * @param height height of screen
	 */
	public SpaceGraphics(int width, int height) throws SlickException {
		this.width = width;
		this.height = height;
		status_x = (int) (0.75 * width);
		System.out.println(status_x);
		status_y = 20;
		background = new Image("spacedata/space_bg_512_424.jpg");
		statusBar = new Image("spacedata/beam.png");
		healthIcon = new Image("spacedata/health.png");
		bg_x = (width - background.getWidth()) / 2;
		bg_y = (height - background.getHeight()) / 2;
		//instructions = new Image(500, 50);
		//Graphics instructionsGfx = instructions.getGraphics();
		//Graphics.setCurrent(instructionsGfx);
		//instructionsGfx.drawString("Press tab when there are only prime numbers left", 0, 0);
		//instructionsGfx.flush();
	}

	
	public void drawBG(Graphics g) {
		g.drawImage(background, bg_x, bg_y);
		//g.drawImage(instructions, 10, height - 50);
	}
	
	/**
	 * Draw a rigid body object to screen
	 * @param b Rigid body
	 * @param g Graphics context
	 */
	public void drawBody(RigidBody b, Graphics g) {
		int xOffset = (int) b.getWidth() / 2;
		int yOffset = (int) b.getHeigth() / 2;

		g.rotate(b.getX(), b.getY(), b.getOrientation('D'));
		if (b.getImage() != null)
			g.drawImage(b.getImage(), b.getX() - xOffset, b.getY() - yOffset);
		g.resetTransform();
		
//		if (b instanceof SpaceShip) {
//			g.drawString("ship pos: " + (int) b.getPos().x + "," + (int) b.getPos().y, 10, 300);
//		}
		
//		for (int i = 0; i < MainGame.debugGfx.length; i++) {
//			if (i % 2 == 1) {
//				g.setColor(Color.yellow);
//			} else {
//				g.setColor(Color.white);
//			};
//			
//			g.draw(MainGame.debugGfx[i]);
//		}
//		g.setColor(Color.white);

		/* debugging box */ 
		//g.draw(b.getShape());

	}
	public void drawFlame(EngineFlame flame) {
		int ang = flame.getOrientation();
		Vector2f pos = flame.getPos();
				
		flame.setTrailEmitter(ang, (int) pos.x, (int) pos.y);		
		flame.getParticleSystem().render();
	}
	
	public void drawLaser(Laser laser, Graphics g) {
		if (!laser.isOn()) {
			return;
		}
		
		float orientation = laser.getOrientation('D');
		float x1 = laser.getRayOriented().getX1();
		float y1 = laser.getRayOriented().getY1();
		
		g.rotate(x1, y1, orientation);
	    laser.getImage().draw(x1, y1, laser.getRayOriented().length(), 8);
	    g.resetTransform();
	}

	public void drawStatus(float strength) {
		healthIcon.draw(status_x-30, status_y);
		statusBar.draw(status_x, status_y, strength, 20);
	}
	
	public void drawMsg(String txt, AngelCodeFont font, Graphics g) {
		g.setFont(font);
		int txtWidth = font.getWidth(txt);
		int txtHeight = font.getLineHeight();
		font.drawString((width - txtWidth) / 2, (height - txtHeight) / 2, txt);
	}
	
	public void drawExplosion(Explosion explosion, Graphics g) {
		explosion.getAnim().draw(explosion.getX(), explosion.getY());
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}
}